//
//  ViewController.m
//  UIHomeWork09-省市区三级页面
//
//  Created by tusm on 16/7/26.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "ViewController.h"
#import "Provience.h"
#import "City.h"
#import "Zone.h"
#import "CityViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *arrOfProvince;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.arrOfProvince = [NSMutableArray array];
    self.navigationItem.title = @"省";
    [self handleData];
    [self createSubView];
}
- (void)handleData{
    NSString *path = @"/Users/tusm/Desktop/课堂代码/UIHomeWork09-省市区三级页面/UIHomeWork09-省市区三级页面/area.txt";
    NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *stringArray = [string componentsSeparatedByString:@"\n"];
    for (NSString *str in stringArray) {
        if (![str hasPrefix:@" "]) {
            Provience *pro = [Provience zoneWithName:str];
            [_arrOfProvince addObject:pro];
        } else if ([str hasPrefix:@" "] && ![str hasPrefix:@"    "]) {
            Provience *pro = [_arrOfProvince lastObject];
            City *city = [City zoneWithName:str];
            [pro.array addObject:city];
        } else {
            City *city = [[[_arrOfProvince lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:str];
            [city.array addObject:zone];
        }
    }

}

- (void)createSubView{

    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.arrOfProvince.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    Provience *pro = _arrOfProvince[indexPath.row];
    cell.textLabel.text = pro.name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    CityViewController *vc = [[CityViewController alloc] init];
    vc.proMedian = _arrOfProvince[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
